<template>
  <div class="order-confirm">
    <div class="wraaper">
      <svg
        version="1.1"
        xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink"
        style="position: absolute; width: 0px; height: 0px; overflow: hidden;"
      >
        <defs>
          <symbol id="icon-add" viewBox="0 0 31 32">
            <title>add</title>
            <path
              d="M30.745 15.152h-14.382v-14.596c0-0.308-0.243-0.557-0.543-0.557s-0.543 0.249-0.543 0.557v14.596h-14.665c-0.3 0-0.543 0.249-0.543 0.557s0.243 0.557 0.543 0.557h14.665v15.177c0 0.307 0.243 0.557 0.543 0.557s0.543-0.249 0.543-0.557v-15.177h14.382c0.3 0 0.543-0.249 0.543-0.557s-0.243-0.557-0.543-0.557z"
              class="path1"
            ></path>
          </symbol>
          <symbol id="icon-edit" viewBox="0 0 32 32">
            <title>edit</title>
            <path
              d="M28.287 8.51l-4.805-4.806 0.831-0.831c0.472-0.472 1.086-0.777 1.564-0.777 0.248 0 0.452 0.082 0.622 0.253l3.143 3.144c0.539 0.54 0.133 1.529-0.524 2.186l-0.831 0.831zM26.805 9.992l-1.138 1.138-4.805-4.806 1.138-1.138 4.805 4.806zM24.186 12.612l-14.758 14.762-4.805-4.806 14.758-14.762 4.805 4.806zM7.379 28.288l-4.892 1.224 1.223-4.894 3.669 3.67zM31.123 4.011l-3.143-3.144c-0.567-0.567-1.294-0.867-2.103-0.867-1.036 0-2.174 0.52-3.045 1.391l-20.429 20.436c-0.135 0.134-0.23 0.302-0.276 0.487l-2.095 8.385c-0.089 0.355 0.017 0.736 0.276 0.995 0.198 0.198 0.461 0.307 0.741 0.307 0.085 0 0.171-0.010 0.254-0.031l8.381-2.096c0.185-0.047 0.354-0.142 0.487-0.276l20.43-20.436c1.409-1.41 2.042-3.632 0.524-5.15v0z"
              class="path1"
            ></path>
          </symbol>
          <symbol id="icon-del" viewBox="0 0 32 32">
            <title>delete</title>
            <path
              d="M11.355 4.129v-2.065h9.29v2.065h-9.29zM6.194 29.935v-23.742h19.613v23.742h-19.613zM30.968 4.129h-8.258v-3.097c0-0.569-0.463-1.032-1.032-1.032h-11.355c-0.569 0-1.032 0.463-1.032 1.032v3.097h-8.258c-0.569 0-1.032 0.463-1.032 1.032s0.463 1.032 1.032 1.032h3.097v24.774c0 0.569 0.463 1.032 1.032 1.032h21.677c0.569 0 1.032-0.463 1.032-1.032v-24.774h3.097c0.569 0 1.032-0.463 1.032-1.032s-0.463-1.032-1.032-1.032v0z"
              class="path1"
            ></path>
            <path
              d="M10.323 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"
              class="path2"
            ></path>
            <path
              d="M16 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"
              class="path3"
            ></path>
            <path
              d="M21.677 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"
              class="path4"
            ></path>
          </symbol>
        </defs>
      </svg>
      <div class="container">
        <div class="order-box">
          <div class="addrsss">
            <h3>收货地址</h3>
            <div class="flex">
              <div
                class="address-info"
                :class="{'active':index==chekIndex}"   
                v-for="(item, index) in list"
                :key="index"
                @click="chekIndex=index"
              >
              <!-- 点击索引的功能 如果点击的索引和v-for里面的索引是一样的的就动态绑定class -->

                <h4>{{ item.receiverName }}</h4>
                <p class="tel">{{ item.receiverMobile }}</p>
                <p class="street">
                  {{
                    item.receiverProvince +
                      "" +
                      item.receiverCity +
                      "" +
                      item.receiverDistrict +
                      "" +
                      item.receiverAddress
                  }}
                </p>
                <div class="action">
                  <a href="javascript:;">
                    <svg class="icon icon-del" @click="delAddress(item)">
                      <use xlink:href="#icon-del"></use>
                    </svg>
                  </a>
                  <a href="javascript:;">
                    <svg class="icon icon-edit" @click="editAddressModal(item)">
                      <use xlink:href="#icon-edit"></use>
                    </svg>
                  </a>
                </div>
              </div>
              <div class="address-add address-info" @click="openAddress">
                <div class="icon-add"></div>
                <div>添加新地址</div>
              </div>
            </div>
          </div>
          <div class="shop">
            <h2>商品</h2>
            <ul>
              <li v-for="(item, index) in cartList" :key="index">
                <div class="shop-name">
                  <img :src="item.productMainImage" alt="" />
                  <span>{{ item.productName+item.productSubtitle }}</span>
                </div>
                <div class="shop-price">
                  {{ item.productPrice }}x{{ item.quantity }}
                </div>
                <div class="shop-total">{{ item.productPrice }}元</div>
              </li>
            </ul>
          </div>
          <div class="peisong">
            <h3>配送方式</h3>
            <span>包邮</span>
          </div>
          <div class="fapiao">
            <h3>发票</h3>
            <a class="javascript:;">电子发票</a>
            <a class="javascript:;">个人</a>
          </div>
          <div class="detail">
            <div class="item">
              <span class="item-name">商品件数：</span>
              <span class="item-num">{{ count }}</span>
            </div>
            <div class="item">
              <span class="item-name">商品总价：</span>
              <span class="item-num">{{ cartTotalPrice }}元</span>
            </div>
            <div class="item">
              <span class="item-name">优惠活动：</span>
              <span class="item-num">0元</span>
            </div>
            <div class="item">
              <span class="item-name">运费：</span>
              <span class="item-num">0元</span>
            </div>
            <div class="item-total">
              <span class="item-name">应付总额：</span>
              <span class="item-num">{{ cartTotalPrice }}元</span>
            </div>
          </div>
          <div class="btn-wrapper">
            <a href="/#/cart" class="btn btn-default btn-large">返回购物车</a>
            <a href="javascript:;" class="btn btn-large" @click="orderSubit">去结算</a>
          </div>
        </div>
      </div>
    </div>
    <modal
      title="删除确认"
      btnType="1"
      :showModal="showDelModal"
      @cancel="showDelModal = false"
      @submit="submitAddress"
    >
      <template #body>
        <p>您确认要删除此地址吗?</p>
      </template>
    </modal>
    <modal
      title="新增确认"
      btnType="1"
      :showModal="showEditModal"
      @cancel="showEditModal = false"
      @submit="submitAddress"
    >
      <template #body>
        <div class="edit-wrap">
          <div class="item">
            <input
              type="text"
              placeholder="姓名"
              class="input"
              v-model="checkedItem.receiverName"
            />
            <input
              type="text"
              placeholder="手机号"
              class="input"
              v-model="checkedItem.receiverMobile"
            />
          </div>
          <div class="item">
            <select name="province" v-model="checkedItem.receiverProvince">
              <option value="北京">北京</option>
              <option value="天津">天津</option>
              <option value="河北">河北</option>
            </select>
            <select name="city" v-model="checkedItem.receiverCity">
              <option value="北京">北京</option>
              <option value="天津">天津</option>
              <option value="河北">石家庄</option>
            </select>
            <select name="district" v-model="checkedItem.receiverDistrict">
              <option value="北京">昌平区</option>
              <option value="天津">海淀区</option>
              <option value="河北">东城区</option>
              <option value="天津">顺义区</option>
              <option value="河北">西城区</option>
            </select>
          </div>
          <div class="item">
            <textarea
              name="street"
              v-model="checkedItem.receiverAddress"
            ></textarea>
          </div>
          <div class="item">
            <input
              type="text"
              placeholder="邮编"
              class="input"
              v-model="checkedItem.receiverZip"
            />
          </div>
        </div>
      </template>
    </modal>
  </div>
</template>

<script>
// import OrderHeader from '../components/OrderHeader'
// import NavFooter from '../components/NavFooter'
import Modal from "../components/Modal";
export default {
  name: "order-confirm",
  components: {
    Modal,
  },
  data() {
    return {
      list: [], //收货地址列表
      cartList: [], //购物车中需要结账的列表
      cartTotalPrice: 0, //商品总金额
      count: 0, //商品总数量
      checkedItem: {}, //当前选中的对象
      userAction: "", //用户行为 0 :新增 1：编辑 2：删除
      showDelModal: false, //是否显示删除弹框
      showEditModal: false, //是否显示新增或者编辑弹框
      chekIndex:0//当前收获地址选中的索引
    };
  },
  mounted() {
    this.getAddressList();
    this.getCartList();
  },
  methods: {
    getAddressList() {
      this.axios.get("/shippings").then((ret) => {
        this.list = ret.list;
      });
    },
    // 新增地址信息
    openAddress() {
      this.userAction = 0;
      this.showEditModal = true;
      this.checkedItem = {};
    },
    //删除地址信息
    delAddress(item) {
      this.checkedItem = item;
      this.userAction = 2;
      this.showDelModal = true;
    },
    // 编辑地址信息
    editAddressModal(item){
      this.checkedItem = item;
      this.userAction = 1;
      this.showEditModal = true;
    },
    // 地址删除，编辑，新增功能
    submitAddress() {
      let { checkedItem, userAction } = this;
      let method,
        url,
        param = {};
      if (userAction == 0) {
        method = "post";
        url = "/shippings";
      } else if (userAction == 1) {
        method = "put";
        url = "/shippings/" + checkedItem.id;
      } else {
        method = "delete";
        url = "/shippings/" + checkedItem.id;
      }
      if (userAction == 0 || userAction == 1) {
        let {
          receiverName,
          receiverMobile,
          receiverProvince,
          receiverCity,
          receiverDistrict,
          receiverAddress,
          receiverZip,
        } = checkedItem;
        let errMsg = "";
        if (!receiverName) {
          errMsg = "请输入收货地址";
        } else if (
          !receiverMobile ||
          !/^(?:(?:\+|00)86)?1\d{10}$/.test(receiverMobile)
        ) {
          errMsg = "请输入正确格式的手机号";
        } else if (!receiverProvince) {
          errMsg = "请选择省份";
        } else if (!receiverCity) {
          errMsg = "请选择城市";
        } else if (!receiverDistrict || !receiverAddress) {
          errMsg = "请输入收货地址";
        }
        if (errMsg) {
          this.$message.error(errMsg);
          return
        }
        param = {
          receiverName,
          receiverMobile,
          receiverProvince,
          receiverCity,
          receiverDistrict,
          receiverAddress,
          receiverZip,
        };
      }
      this.axios[method](url,param).then(() => {
        this.closeModal();
        this.getAddressList();
        // 重新拉取一次代码 可能涉及到并发
        this.$message.success("操作成功");
      });
    },
    closeModal() {
      this.checkedItem = '';
      this.userAction = "";
      this.showDelModal = false;
      this.showEditModal=false
    },
    getCartList() {
      this.axios.get("/carts").then((ret) => {
        let list = ret.cartProductVoList;
        this.cartTotalPrice = ret.cartTotalPrice;
        this.cartList = list.filter((item) => item.productSelected);
        this.cartList.map((item) => {
          // 遍历这个数组 有一条数据累加为一
          this.count += item.quantity;
        });
      });
    },
    orderSubit(){
      let item=this.list[this.chekIndex]
      if(!item){
        this.$message.error('请选择一个收货地址')
        return
      }
      this.axios.post('/orders',{
        shippingId:item.id
      }).then((ret) => {
        this.$router.push({
          path:'/order/pay',
          query:{
            orderNo:ret.orderNo
          }
        })
      })
    }
  },
};
</script>

<style lang="scss" scoped>
@import "../assets/scss/mixin.scss";
@import "../assets/scss/config.scss";
@import "../assets/scss/base.scss";
@import "../assets/scss/button.scss";
.order-confirm {
  .wraaper {
    background-color: #f5f5f5;
    border-top: 2px solid $colorA;
    padding-top: 50px;
    padding-bottom: 50px;
    .order-box {
      background-color: #fff;
      padding-left: 40px;
      padding-bottom: 40px;

      .addrsss {
        padding-top: 30px;
        h3 {
          font-size: 20px;
          color: #333;
          font-weight: 200;
          margin-bottom: 20px;
        }
        .active {
          border: 1px solid $colorA;
        }
        .flex {
          display: flex;
          .address-info {
            cursor: pointer;
            box-sizing: border-box;
            width: 271px;
            height: 180px;
            border: 1px solid #e5e5e5;
            padding: 15px 24px;
            font-size: 14px;
            color: #757575;
            margin-right: 10px;
            h4 {
              height: 27px;
              font-size: 18px;
              font-weight: 300;
              color: #333;
              margin-bottom: 10px;
            }
            .street {
              height: 60px;
            }
            .action {
              display: flex;
              justify-content: space-between;
              a {
                svg {
                  width: 20px;
                  height: 20px;
                }
              }
            }
          }
          .active {
            border: 1px solid $colorA;
          }
          .address-add {
            text-align: center;
            .icon-add {
              width: 30px;
              height: 30px;
              border-radius: 50%;
              background: url(/imgs/icon-add.png) #e0e0e0 no-repeat 50%;
              margin: 45px auto 10px;
            }
          }
        }
      }
      .shop {
        margin-top: 30px;
        h2 {
          border-bottom: 1px solid #e5e5e5;
          padding-bottom: 5px;
        }
        li {
          display: flex;
          align-items: center;
          height: 40px;
          line-height: 40px;
          margin-top: 10px;
          font-size: 16px;
          border-bottom: 1px solid #e5e5e5;
          padding: 10px 0;
          color: #333;
          .shop-name {
            flex: 5;
            img {
              width: 30px;
              height: 30px;
              vertical-align: middle;
              margin-right: 30px;
            }
          }
          .shop-price {
            flex: 2;
          }
          .shop-total {
            flex: 2;
            color: $colorA;
          }
        }
      }
      .peisong {
        margin-top: 30px;
        line-height: 20px;
        h3 {
          display: inline-block;
          margin-right: 71px;
          font-size: 20px;
          width: 80px;
        }
        span {
          color: $colorA;
          font-size: 16px;
          margin-right: 23px;
        }
      }
      .fapiao {
        margin-top: 30px;
        line-height: 20px;
        h3 {
          display: inline-block;
          margin-right: 71px;
          font-size: 20px;
          width: 80px;
        }
        a {
          color: $colorA;
          font-size: 16px;
          margin-right: 23px;
        }
      }
      .detail {
        padding: 50px 44px 33px 0;
        border-bottom: 1px solid #f5f5f5;
        text-align: right;
        font-size: 16px;
        color: #666;
        .item {
          line-height: 15px;
          margin-bottom: 12px;
          .item-num {
            display: inline-block;
            width: 100px;
            color: $colorA;
          }
        }
        .item-total {
          line-height: 15px;
          margin-bottom: 12px;
          margin-top: 20px;
          .item-num {
            display: inline-block;
            width: 100px;
            color: $colorA;
            font-size: 28px;
          }
        }
      }
      .btn-wrapper {
        margin-top: 37px;
        text-align: right;
      }
    }
  }
  .edit-wrap {
    font-size: 14px;
    .item {
      margin-bottom: 15px;
      .input {
        box-sizing: border-box;
        display: inline-block;
        width: 283px;
        height: 40px;
        line-height: 40px;
        padding-left: 15px;
        border: 1px solid #e5e5e5;
        & + .input {
          margin-left: 14px;
        }
      }
      select {
        height: 30px;
        line-height: 30px;
        border: 1px solid #e5e5e5;
        margin-right: 15px;
      }
      textarea {
        height: 62px;
        width: 100%;
        padding: 13px 15px;
        box-sizing: border-box;
        border: 1px solid #e5e5e5;
      }
    }
  }
}
</style>
